package cn.bluejoe.elfinder.servlet;

import cn.bluejoe.elfinder.controller.ConnectorController;
import cn.bluejoe.elfinder.controller.executor.CommandExecutorFactory;
import cn.bluejoe.elfinder.controller.executor.DefaultCommandExecutorFactory;
import cn.bluejoe.elfinder.controller.executors.MissingCommandExecutor;
import cn.bluejoe.elfinder.impl.*;
import cn.bluejoe.elfinder.localfs.LocalFsVolume;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * ConnectorServlet is an example servlet
 * it creates a ConnectorController on init() and use it to handle requests on doGet()/doPost()
 * 
 * users should extend from this servlet and customize required protected methods
 * 
 * @author bluejoe
 *
 */
public class ConnectorAdminServlet extends HttpServlet
{
	// core member of this Servlet
	ConnectorController _connectorController;

	/**
	 * create a command executor factory
	 * 
	 * @param config
	 * @return
	 */
	protected CommandExecutorFactory createCommandExecutorFactory(
			ServletConfig config)
	{
		DefaultCommandExecutorFactory defaultCommandExecutorFactory = new DefaultCommandExecutorFactory();
		defaultCommandExecutorFactory
				.setClassNamePattern("cn.bluejoe.elfinder.controller.executors.%sCommandExecutor");
		defaultCommandExecutorFactory
				.setFallbackCommand(new MissingCommandExecutor());
		return defaultCommandExecutorFactory;
	}

	/**
	 * create a connector controller
	 * 
	 * @param config
	 * @return
	 */
	protected ConnectorController createConnectorController(ServletConfig config)
	{
		ConnectorController connectorController = new ConnectorController();

		connectorController
				.setCommandExecutorFactory(createCommandExecutorFactory(config));
		connectorController.setFsServiceFactory(createServiceFactory(config));

		return connectorController;
	}

	protected DefaultFsService createFsService()
	{
		DefaultFsService fsService = new DefaultFsService();
		fsService.setSecurityChecker(new FsSecurityCheckForAll());

		DefaultFsServiceConfig serviceConfig = new DefaultFsServiceConfig();
		serviceConfig.setTmbWidth(80);

		fsService.setServiceConfig(serviceConfig);

		fsService.addVolume("A",
				createLocalFsVolume("共享文件", new File("/files1/a")));
		/*fsService.addVolume("B",
				createLocalFsVolume("共享文件", new File("/files1/b")));*/

		//权限控制
		FsSecurityCheckForAll _fsSecurityCheckForAllWR = new FsSecurityCheckForAll();
		_fsSecurityCheckForAllWR.setReadable(true);
		_fsSecurityCheckForAllWR.setWritable(true);
		_fsSecurityCheckForAllWR.setLocked(false);
		/*FsSecurityCheckForAll _fsSecurityCheckForAllRO = new FsSecurityCheckForAll();
		_fsSecurityCheckForAllRO.setReadable(true);
		_fsSecurityCheckForAllRO.setWritable(false);
		_fsSecurityCheckForAllRO.setLocked(true);*/

		FsSecurityCheckFilterMapping _fsSecurityCheckFilterMappingWR = new FsSecurityCheckFilterMapping();
		_fsSecurityCheckFilterMappingWR.setPattern("A_.*");
		_fsSecurityCheckFilterMappingWR.setChecker(_fsSecurityCheckForAllWR);

		/*FsSecurityCheckFilterMapping _fsSecurityCheckFilterMappingRO = new FsSecurityCheckFilterMapping();
		_fsSecurityCheckFilterMappingRO.setPattern("B_.*");
		_fsSecurityCheckFilterMappingRO.setChecker(_fsSecurityCheckForAllRO);*/

		FsSecurityCheckerChain _fsSecurityCheckerChain = new FsSecurityCheckerChain();
		List<FsSecurityCheckFilterMapping> _filterMappings = new ArrayList<FsSecurityCheckFilterMapping>();
		_filterMappings.add(_fsSecurityCheckFilterMappingWR);
		/*_filterMappings.add(_fsSecurityCheckFilterMappingRO);*/
		_fsSecurityCheckerChain.setFilterMappings(_filterMappings);
		fsService.setSecurityChecker(_fsSecurityCheckerChain);

		return fsService;
	}


	private LocalFsVolume createLocalFsVolume(String name, File rootDir)
	{
		LocalFsVolume localFsVolume = new LocalFsVolume();
		localFsVolume.setName(name);
		localFsVolume.setRootDir(rootDir);
		return localFsVolume;
	}

	/**
	 * create a service factory
	 * 
	 * @param config
	 * @return
	 */
	protected StaticFsServiceFactory createServiceFactory(ServletConfig config)
	{
		StaticFsServiceFactory staticFsServiceFactory = new StaticFsServiceFactory();
		DefaultFsService fsService = createFsService();

		staticFsServiceFactory.setFsService(fsService);
		return staticFsServiceFactory;
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		_connectorController.connector(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		_connectorController.connector(req, resp);
	}

	@Override
	public void init(ServletConfig config) throws ServletException
	{
		_connectorController = createConnectorController(config);
	}
}
